package com.eascs.banknet.boc.net.mock;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.banknet.boc.base.BOCUtil;
import com.eascs.banknet.boc.dao.BOC002Dao;
import com.eascs.banknet.boc.dao.DataListDao;
import com.eascs.banknet.boc.dao.LoanapplyDao;
import com.eascs.banknet.boc.net.BOCNetService;
import com.eascs.banknet.boc.vo.FtpFileTemplage;
import com.eascs.banknet.boc.vo.boc002.BOC002Element;
import com.eascs.banknet.common.entity.DataListEntity;
import com.eascs.banknet.common.entity.LoanUserEntity;
import com.eascs.banknet.common.vo.ServerInfo;
import com.eascs.banknet.constant.FileTypeConstants;
import com.eascs.common.util.StringUtil;

@Service("bocNetServiceMock")
public class BOCNetServiceMock implements BOCNetService {
	@Autowired
	private  BOCUtil bocUtil;
	@Autowired 
	private LoanapplyDao loanapplyDao;
	@Autowired
	private DataListDao dataListDao;
	
	@Autowired
	private BOC002Dao boc002Dao;
	
	@Override
	public String executeSocket(ServerInfo serverInfo, String type,String appno,  String content) {
		String result = null;
		switch (type) {
		case "B01":
			appno = getRandomAppno();
			result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><RETCODE>0000</RETCODE><NOTE></NOTE><OSN>1607141707470980000</OSN></HEADER><BODY><OUTPUT><APPNO>"
					+ appno + "</APPNO></OUTPUT><INPUT></INPUT></BODY></ROOT>";
			break;
		case "B02":
			result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><RETCODE>0000</RETCODE><NOTE></NOTE><OSN>1607221541290770000</OSN></HEADER><BODY><OUTPUT><APPNO>"
					+ appno + "</APPNO><CREDITLIMITAMT>191573.0</CREDITLIMITAMT><CREDITOVERLIMIT6M>101404.0</CREDITOVERLIMIT6M><CREDITLIMITRATE>31.81</CREDITLIMITRATE><LOANDETAIL><SEQNO>1</SEQNO><LOANTYPE>个人经营性贷款</LOANTYPE><STATUS>正常</STATUS><LOANAMT>40000.0</LOANAMT><REMAINAMT>17626.0</REMAINAMT><LOANENDDT>20160107</LOANENDDT><REPAYAMTOFMONTH>3707.0</REPAYAMTOFMONTH><ORGTYPE>小额信贷公司</ORGTYPE><GUARSTYLE>信用/免担保</GUARSTYLE><FIVECLASS>正常</FIVECLASS></LOANDETAIL><LOANDETAIL><SEQNO>2</SEQNO><LOANTYPE>个人经营性贷款</LOANTYPE><STATUS>正常</STATUS><LOANAMT>170000.0</LOANAMT><REMAINAMT>87153.0</REMAINAMT><LOANENDDT>20160113</LOANENDDT><REPAYAMTOFMONTH>22487.0</REPAYAMTOFMONTH><ORGTYPE>商业银行</ORGTYPE><GUARSTYLE>其他</GUARSTYLE><FIVECLASS>正常</FIVECLASS></LOANDETAIL></OUTPUT><INPUT></INPUT></BODY></ROOT>";
			break;
		case "S01":
			result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><RETCODE>0000</RETCODE><NOTE></NOTE><OSN>1607141909433170000</OSN></HEADER><BODY><OUTPUT><CREDITCAL><APPNO>"
					+ appno + "</APPNO><CREDITLINE>0.0</CREDITLINE><CREDITRANGE>0.0</CREDITRANGE></CREDITCAL></OUTPUT><INPUT></INPUT></BODY></ROOT>";
			break;
		case "S02":
			result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><RETCODE>0000</RETCODE><NOTE></NOTE><OSN>1607281407148660000</OSN></HEADER><BODY><OUTPUT><APPNO>"
					+ appno + "</APPNO></OUTPUT><INPUT></INPUT></BODY></ROOT>";
			break;
		case "001":
			result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><RETCODE>0000</RETCODE><NOTE></NOTE><OSN>1607221700484830000</OSN></HEADER><BODY><OUTPUT><APPNO>"
					+ appno + "</APPNO></OUTPUT><INPUT></INPUT></BODY></ROOT>";
			break;
		case "002":
			List<BOC002Element> dataList = boc002Dao.queryData("");
			StringBuffer taskLogs = new StringBuffer();
			for(BOC002Element element:dataList){
				LoanUserEntity entity = loanapplyDao.getLoanUserByAppno(element.getAPPNO());
				String status = "";
				if("1".equals(entity.getBankStatus())){
					status = "01B3";
				}
				if("2".equals(entity.getBankStatus())){
					status = "0149";
				}
				taskLogs.append("<TASKLOG><APPNO>").append(element.getAPPNO())
				.append("</APPNO><LOAN_PER>杨红丽</LOAN_PER><CERTY>01</CERTY><CERNO>412926197205234920</CERNO><STATUS>")
				.append(status)
				.append("</STATUS><ACTNO></ACTNO><CCAS_SEQNO></CCAS_SEQNO></TASKLOG>");
			}
			result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><RETCODE>0000</RETCODE><NOTE></NOTE><OSN>1607141725316130000</OSN></HEADER><BODY><OUTPUT>"
					+ taskLogs.toString() +"</OUTPUT><INPUT></INPUT></BODY></ROOT>";
			break;
		case "003":
			result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><RETCODE>0000</RETCODE><NOTE></NOTE><OSN>1607281424424130000</OSN></HEADER><BODY><OUTPUT><LOANINFO><APPNO>"
					+ appno +"</APPNO><LOAN_FLG>Y</LOAN_FLG><TRF_ACCT_NO>00000000000000000</TRF_ACCT_NO><LOAN_PER>徐振华</LOAN_PER><CERTY>01</CERTY><CERNO>339005198111176635</CERNO><ACTNO>00000767962153293</ACTNO><LOAN_AMT>95000.00</LOAN_AMT><RATE>9.225</RATE><FADAT>20131220</FADAT><LAFTERM>036</LAFTERM><MATURITY_DATE>20161220</MATURITY_DATE><REPAY_SCHED>F</REPAY_SCHED></LOANINFO></OUTPUT><INPUT></INPUT></BODY></ROOT>";
			break;
		case "004":
			result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ROOT><HEADER><TRANNO>004</TRANNO><BNKNO>17856</BNKNO><COMNO>COM05</COMNO><OSN>1607221404512642000</OSN></HEADER><BODY><TRANLOG><APPNO>0000000181</APPNO><NOTATION>同意受理</NOTATION><TYPE>7</TYPE></TRANLOG></BODY></ROOT>";
			break;
		default:
			break;
		}
		return result;
	}
	
	private String getRandomAppno(){
		boolean flag = true;
		while(flag){
			long number = Math.round(Math.random()*9999);
			String appno = String.format("%09d", number);
			LoanUserEntity entity = loanapplyDao.getLoanUserByAppno(appno);
			if(entity == null){
				flag = false;
				return appno;
			}
		}
		return null;
	}
	
	@Override
	public boolean getFtpAttachment(FtpFileTemplage ftpFile, String appno, String loanapplyNo, String bankOsn) {
		String fileType = ftpFile.getFileType();
		String urlPath = "",extendname = "html",originalname="";
		List<DataListEntity> dataList = new ArrayList<>();
		DataListEntity dataListEntity = new DataListEntity();
		dataListEntity.setLoanapplyNo(loanapplyNo);
		dataListEntity.setExtendNmae(extendname);
		dataListEntity.setCreatDate(new Date());
		dataListEntity.setDatalistName(ftpFile.getFileDesc());
		dataListEntity.setDatalistCode(ftpFile.getFileType());
		if(FileTypeConstants.DALIST_TYPE_ZXBGDATA.equals(fileType)
				|| FileTypeConstants.DALIST_TYPE_ZXMATE.equals(fileType)){
			dataListEntity.setOriginalName("信用报告.html");
			dataListEntity.setUrlpath("ZXBGDATA/16/07/14684829930337m.html");
			dataList.add(dataListEntity);
		}
		if(FileTypeConstants.DALIST_TYPE_REFUNDCONTRACTBAK.equals(fileType)){
			dataListEntity.setExtendNmae("pdf");
			dataListEntity.setOriginalName("Contract.pdf");
			dataListEntity.setUrlpath("REFUNDCONTRACTBAK/16/07/1469761080752gn.pdf");
			dataList.add(dataListEntity);
			dataListEntity.setOriginalName("ReceLoan.pdf");
			dataListEntity.setUrlpath("REFUNDCONTRACTBAK/16/07/1469761082815w8.pdf");
			dataList.add(dataListEntity);
		}
		//检查文件是否已下载
		int count = dataListDao.getFileCount(loanapplyNo, ftpFile.getFileType());
		if(count > 0) {
			return true;
		}
		for(DataListEntity entity:dataList){
			if(StringUtil.hasText(entity.getUrlpath())){
				dataListDao.insert(entity);
			}
		}
		return true;
	}
	
	@Override
	public boolean sendFtpAttachment(String appno,
			List<DataListEntity> fileList) {
		return true;
	}
	
	@Override
	public ByteArrayInputStream readFtpFile(ServerInfo serverInfo, String remotePath, String fileName) {
		// TODO Auto-generated method stub
		return null;
	}
}

